from eth_utils import encode_hex

from protocal.did import DID
from client.signer_impl import Signer_Impl

def test_did(didobj,setparam=False):
    if setparam:
        #设置几个内置属性
        didobj.params.service = "svc1"
        didobj.params.versionId='1.0.0.1x'
        didobj.params.relativeRef = "http://127.0.0.01/#a"
    print("start did detail:", didobj.detail())
    print(didobj.query)
    print("update query:",didobj.query)
    didstr=didobj.dump()
    print("dump didstr:",didstr)
    print(">>>>>>>>>>>>>>>>>test load did from string")
    didobj2 = DID.loads(didstr)
    print("load did deteil:",didobj2.detail())
    print("dump_id:",didobj2.dump_id())
    print("dump_url:",didobj2.dump_url())
    print(didobj2.dump())



if True:
    from eth_account import Account
    from client.gm_account import GM_Account

    key = "7a94d9793bcc38f533c6e15d8ef9c557e8ead2d3f86e9ac1178ce56b2815f86b"
    # key = "6eb7d91a86ee378defd30ef8abb9b72499845c05846a2d408583f0e1a4903f5d"
    ethAccount :Account= Account.from_key(key)
    id = ethAccount.address

    gmAccount = GM_Account()
    gmAccount.from_key(key)
    id =  gmAccount.keypair.address
    didobj = DID(id,scheme="weid100",method="test101",url="/root/content?query=true&key=unk+8&key=abc&service=unknow#idx001")
    #didobj = DID("0x1234567890", scheme="weid100", method="test101")
    #didobj = DID("0x1234567890", scheme="weid100", method="test101",url="#pos=1")
    test_did(didobj)
    didstr = bytes(didobj.dump(),"UTF-8")
    from eth_utils.crypto import keccak
    signres =ethAccount.signHash(keccak(didstr))
    print("ECDSA Sign->",signres)
    si = Signer_Impl()
    sig = si.vrs_to_sig((signres.v,signres.r,signres.s))
    print("sig1->",sig)
    print("sig2->",encode_hex(signres.signature))
    from client.signer_impl import Signer_GM, Signer_Impl

    gm_signer = Signer_GM(gmAccount)
    from client.signer_impl import gmhash
    signres= gm_signer.sign(gmhash(didstr),False)
    print("GM Sign->",signres)
    sig = gm_signer.vrs_to_sig(signres,with_v=False,with_0x_prefix=True)
    print("sig ->",sig)
    print("gm v -> 0x%064x"%signres[0])
    print("gm account ->",gmAccount.keypair.public_key)